package cn.fancylab.template.web.action.manager.goods;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.fancylab.model.acl.AuthUser;
import cn.fancylab.model.bs.BaseGoods;
import cn.fancylab.service.DaoService;
import cn.fancylab.template.util.JsonUtil;
import cn.fancylab.template.vo.TableListVo;
import cn.fancylab.template.web.action.admin.BaseAdminController;
import cn.fancylab.web.component.acl.SecurityUtil;
import cn.fancylab.web.support.FancyVelocityView;
import cn.fancylab.web.support.JsonStrDirectOut;

@Controller
public class BaseGoodsController extends BaseAdminController implements InitializingBean {
	@Autowired
	@Qualifier("daoService")
	private DaoService daoService;

	@RequestMapping(value = "/manager/goods/basegoods/list.xhtml")
	public String list(ModelMap model) {
		return "manager/goods/basegoods/list.vm";
	}

	@RequestMapping(value = "/manager/goods/basegoods/list_data.xhtml")
	public String list_data(ModelMap model) {
		HttpServletRequest request = getRequest();

		int pagesize = 10;
		int page = 0;
		String currentpage = request.getParameter("page");
		String limit = request.getParameter("limit");
		String goods_name = request.getParameter("goods_name");

		if (StringUtils.isNotBlank(currentpage)) {
			page = Integer.valueOf(currentpage) - 1;
		}
		if (StringUtils.isNotBlank(limit)) {
			pagesize = Integer.valueOf(limit);
		}
		DetachedCriteria query = DetachedCriteria.forClass(BaseGoods.class);
		query.add(Restrictions.eq("is_del", 0));
		if (StringUtils.isNotBlank(goods_name)) {
			query.add(Restrictions.like("goods_name", "%".concat(goods_name).concat("%")));
		}
		int count = daoService.getObjectCountByCriteria(query);
		query.setProjection(null);
		List<BaseGoods> list = daoService.findByCriteria(query, page * pagesize, pagesize);

		TableListVo<BaseGoods> tableListVo = new TableListVo<BaseGoods>();
		tableListVo.setCode(0);
		tableListVo.setMsg("");
		tableListVo.setCount(count + "");
		tableListVo.setData(list);

		request.setAttribute(FancyVelocityView.RENDER_JSON, "true");
		request.setAttribute(FancyVelocityView.DIRECT_OUTPUT,
				new JsonStrDirectOut(getResponse(), JsonUtil.serialize(tableListVo)));
		return "common/directOut.vm";
	}

	@RequestMapping(value = "/manager/goods/basegoods/add.xhtml")
	public String add(String uuid) {
		return "manager/goods/basegoods/form.vm";
	}

	@RequestMapping(value = "/manager/goods/basegoods/edit.xhtml")
	public String edit(String uuid, ModelMap model) {
		BaseGoods baseGoods = daoService.getObject(BaseGoods.class, uuid);
		model.put("baseGoods", baseGoods);
		return "manager/goods/basegoods/form.vm";
	}

	@RequestMapping(value = "/manager/goods/basegoods/save.xhtml")
	public String save(BaseGoods baseGoodsParameter) {
		AuthUser loginUser = SecurityUtil.getLogonUser();

		BaseGoods baseGoods = daoService.getObject(BaseGoods.class, baseGoodsParameter.getUuid());

		if (null == baseGoods) {
			baseGoods = new BaseGoods();
			baseGoods.setUuid(UUID.randomUUID().toString());
			baseGoods.setIs_del(0);
			baseGoods.setAdd_date(new Date());
			baseGoods.setAdd_user_id(loginUser.getId());
		} else {
			baseGoods.setUp_date(new Date());
			baseGoods.setUp_user_id(loginUser.getId());
		}
		baseGoods.setGoods_name(baseGoodsParameter.getGoods_name());
		baseGoods.setMemo(baseGoodsParameter.getMemo());
		daoService.saveObject(baseGoods);

		HttpServletRequest request = getRequest();
		request.setAttribute(FancyVelocityView.RENDER_JSON, "true");
		request.setAttribute(FancyVelocityView.DIRECT_OUTPUT,
				new JsonStrDirectOut(getResponse(), JsonUtil.serialize(baseGoods)));
		return "common/directOut.vm";
	}

	@RequestMapping(value = "/manager/goods/basegoods/del.xhtml")
	public String del(String[] uuid) {
		AuthUser loginUser = SecurityUtil.getLogonUser();

		BaseGoods baseGoods;
		for (int i = 0, len = uuid.length; i < len; i++) {
			if (StringUtils.isNotBlank(uuid[i])) {
				baseGoods = daoService.getObject(BaseGoods.class, uuid[i]);

				if (null != baseGoods) {
					baseGoods.setIs_del(1);
					baseGoods.setDel_date(new Date());
					baseGoods.setDel_user_id(loginUser.getId());
					daoService.saveObject(baseGoods);
				}
			}
		}

		HttpServletRequest request = getRequest();
		request.setAttribute(FancyVelocityView.RENDER_JSON, "true");
		request.setAttribute(FancyVelocityView.DIRECT_OUTPUT,
				new JsonStrDirectOut(getResponse(), JsonUtil.serialize(new HashMap())));
		return "common/directOut.vm";
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		// TODO Auto-generated method stub

	}
}
